package com.selfdiscipline.data.local

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.selfdiscipline.data.model.Habit
import com.selfdiscipline.data.model.HabitCompletion
import com.selfdiscipline.data.model.Task
import com.selfdiscipline.data.model.TimeRecord
import com.selfdiscipline.data.model.FocusSettings
import com.selfdiscipline.data.model.BlockedApp
import com.selfdiscipline.data.local.dao.HabitDao
import com.selfdiscipline.data.local.dao.TaskDao
import com.selfdiscipline.data.local.dao.TimeRecordDao
import com.selfdiscipline.data.local.entity.HabitEntity
import com.selfdiscipline.data.local.entity.TaskEntity
import com.selfdiscipline.data.local.entity.TimeRecordEntity
import com.selfdiscipline.data.local.util.Converters

/**
 * Main database for the application
 */
@Database(
    entities = [
        TaskEntity::class,
        HabitEntity::class,
        TimeRecordEntity::class
    ],
    version = 1,
    exportSchema = false
)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
    
    abstract fun taskDao(): TaskDao
    
    abstract fun habitDao(): HabitDao
    
    abstract fun timeRecordDao(): TimeRecordDao
    
    companion object {
        private const val DATABASE_NAME = "self-discipline-db"
        
        @Volatile
        private var INSTANCE: AppDatabase? = null
        
        fun getDatabase(context: Context): AppDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    DATABASE_NAME
                )
                .fallbackToDestructiveMigration()
                .build()
                INSTANCE = instance
                instance
            }
        }
    }
} 